﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Business.Fa;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Fa
{
    public partial class frmManageAssetType : BaseForm.frmBaseMdiBusinessForm
    {

        #region Variable
        private FaAssetsTypeEntity mSelectedAssetType;
        #endregion

        #region Constructor
        public frmManageAssetType()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private string GetCondition()
        {
            string condition = "";
            if (txtParentType.Tag != null && txtParentType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                FaAssetsTypeEntity parent = (FaAssetsTypeEntity)txtParentType.Tag;
                condition += " AND AT1.PARENT_ID = '" + parent.AssetTypeId + "' ";
            }
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND AT1.ASSET_TYPE_CODE LIKE '%" + txtTypeCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND AT1.ASSET_TYPE_NAME LIKE '%" + txtTypeName.Text + "%' ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ASSET_TYPE_CODE";
            DataTable dt = FaAssetsTypeBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAssetType = new FaAssetsTypeEntity(dr);
        }

        private void txtParentType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm=new frmMaintainAssetTypeQuery();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK && frm.SelectedAssetType != null)
            {
                txtParentType.Text = frm.SelectedAssetType.AssetTypeName;
                txtParentType.Tag = frm.SelectedAssetType;
            }
            else
            {
                txtParentType.Text = "";
                txtParentType.Tag = null;
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void  Click_View()
        {
            if (mSelectedAssetType == null)
            {
                MessageBox.Show("请选择一个固定资产类别", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetType frm = new frmMaintainAssetType();
            frm.ThisActionType = ActionType.View;
            frm.SelectedAssetType = mSelectedAssetType;
            frm.ShowDialog();

        }

        protected override void Click_New()
        {
            frmMaintainAssetType frm = new frmMaintainAssetType();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedAssetType == null)
            {
                MessageBox.Show("请选择一个固定资产类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetType frm = new frmMaintainAssetType();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedAssetType = mSelectedAssetType;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedAssetType == null)
            {
                MessageBox.Show("请选择一个固定资产类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("删除时，子类将一起被删除，是否确定删除选中固定资产类别？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FaAssetsTypeBs.Delete(mSelectedAssetType);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion
    }
}